var https = require('https');
var bingProperties = require('./bingProperties');
var ET = bingProperties.ET;
var $ = require('jquery');
var fs = require('fs');

var SOAP = bingProperties.SOAP;
var STATIC = bingProperties.STATIC;


exports.send = function(option, soapStr, callback) {
    var req = https.request(option, function(res) {
        res.on('data', function(data) {
            console.log('event data');
            var xmlTree = ET.parse(data.toString());
            var fault = $.extend(true, {}, bingProperties.ErrorObj);
            switch(res.statusCode) {
            case 500:
                var faultTree = ET.parse(data.toString());
                fault.faultcode = faultTree.find(".//faultcode").text;
                fault.faultstring = faultTree.find(".//faultstring").text;
                if(null != faultTree.find(".//AdApiFaultDetail")) {
                    fault.detail.AdApiFaultDetail.TrackingId = faultTree.findtext(".//TrackingId");
                    // Display AdApiErrors.
                    errorList = faultTree.findall(".//AdApiError");
                    $.each(errorList, function(idx, error) {
                        var AdApiError = {
                            Message: error.find("Message").text,
                            Detail: error.find("Detail").text,
                            ErrorCode: error.find("ErrorCode").text,
                            Code: error.find("Code").text
                        };
                        //add fault obj
                        fault.detail.AdApiFaultDetail.Errors.push(AdApiError);
                    });
                } else if(null != faultTree.find(".//ApiFaultDetail")) {
                    // Display operation errors.
                    fault.detail.ApiFaultDetail.TrackingId = faultTree.findtext(".//TrackingId");
                    operationErrorList = faultTree.findall(".//OperationError");
                    $.each(operationErrorList, function(idx, error) {
                        var operationError = {
                            Message: error.find("Message").text,
                            Details: error.find("Details").text,
                            ErrorCode: error.find("ErrorCode").text,
                            Code: error.find("Code").text
                        };
                        //add fault obj
                        fault.detail.ApiFaultDetail.OperationErrors.push(operationError);
                    });
                    // Display batch errors.
                    batchErrorList = faultTree.findall(".//BatchError");
                    $.each(batchErrorList, function(idx, error) {
                        var batchError = {
                            Index: error.find("Index").text,
                            Message: error.find("Message").text,
                            Details: error.find("Details").text,
                            ErrorCode: error.find("ErrorCode").text,
                            Code: error.find("Code").text
                        };
                        fault.detail.ApiFaultDetail.BatchErrors.push(operationError);
                    });
                }
                fault.xml = data;
                if(callback) {
                    // console.log(fault);
                    callback(fault, null);
                }
                break;
            case 200:
                fault = null;
                if(callback) {
                    // console.log(fault);
                    callback(fault, data);
                }
                break;
            default:
                throw new Error('unknow error occured');
                // console.log('service request ' + res.statusCode);
                // break;
            }

        });
        res.on('end', function(e) {
            console.log('event end');
        });
        res.on('close', function(e) {
            console.log('event close');
        });
        // console.log('request',res.statusCode,res);
    });
    req.write(soapStr);
    req.end();

    req.on('response', function(response) {
        // console.log('response', response);
        console.log('event response',response.statusCode);

        // res.on('data', callback);
    });
    req.on('socket', function(socket) {
        // console.log('socket', socket)
        console.log('event socket');
    });
    req.on('connect', function(response, socket, head) {
        // console.log('connect', response, socket, head)
        console.log('event connect');
    });
    req.on('upgrade', function(response, socket, head) {
        // console.log('upgrade', response, socket, head)
        console.log('event upgrade');
    });
    req.on('continue', function() {
        console.log('event continue');
    });
    req.on('error', function(e) {
        console.error('error', e);
    });
};